home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / MiniEdit / Mini Edit Movie Stuff.c < prev    next >
C/C++ Source or Header  |  1991-09-11  |  15KB  |  529 lines

  1. /**************************************************
  2. *
  3. * Moov stuff
  4. *
  5. *
  6. ***************************************************/
  7.  
  8. #include "Mini Edit.h"
  9.  
  10. #include "Movies.h"
  11.  
  12. extern    MovieInstance    movieList[];
  13.  
  14. /**************************************************
  15. *
  16. * Globals
  17. *
  18. ***************************************************/
  19.  
  20. /*    Info about the movie */
  21. MovieInstance    *activeMovie;            /* The frontmost movie */
  22. short            numOpenMovies;            /* The number of open movies */
  23. short            checkMovieControllerCount;    /* Filter proc uses this instead of rescanning movie list */
  24. short                theModifiers;            /* Modifiers from the event record for MovieController filter */
  25. OSErr            theErr;                    /* Easier than declaring it everywhere */
  26.  
  27.  
  28. /**************************************************
  29. *
  30. * SetUpMovies() Initializes the movie tools
  31. *
  32. ***************************************************/
  33. void SetUpMovies()
  34.  
  35. {
  36.     short    movieCount;
  37.     
  38.     theErr = EnterMovies();        /* This would normally with the other manager inits */
  39.     if (theErr)
  40.         DebugStr((StringPtr)"\pEnterMovies Failed");
  41.  
  42.     /* Clear the array of open movies */
  43.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  44.         movieList[movieCount].movie = 0;
  45.     activeMovie = 0;
  46.     numOpenMovies = 0;                        /* None currently open */
  47.     
  48. }
  49.  
  50.  
  51. /**************************************************
  52. *
  53. * GetAvailMovie() 
  54. *    Returns a pointer to an unused element in movieList
  55. *    or 0 if none available
  56. *
  57. ***************************************************/
  58. MovieInstance* GetAvailMovie()
  59.  
  60. {
  61.     short movieCount;
  62.     
  63.     /* Loop through looking for a match */
  64.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  65.         if (movieList[movieCount].movie==0)
  66.             break;
  67.     if (movieCount < maxMovies)
  68.         return(&movieList[movieCount]);
  69.                 
  70.     return(0L);
  71.  
  72. }
  73.     
  74.     
  75. /**************************************************
  76. *
  77. * CleanUpMovie(theMovie) Throws out the movie, window, and controller
  78. *
  79. ***************************************************/
  80. void CleanUpMovie(MovieInstance    *theMovie)
  81.  
  82. {
  83.     if(theMovie->movie)                            /* if 0, this entry is unused */
  84.     {
  85.         CloseMovieFile(theMovie->resRefNum);    /* We're done with the file */
  86.  
  87.         CloseComponent(theMovie->movieController); /* Throw out the controller first */
  88.         theMovie->movieController = nil;
  89.         DisposeMovie(theMovie->movie);            /* Toss the movie */
  90.         theMovie->movie = 0;                    /* Mark it as unused */
  91.         if(theMovie == activeMovie)
  92.             activeMovie=0;
  93.         DisposeWindow(theMovie->window);        /* Toss the window, too */
  94.         numOpenMovies--;                        /* One less movie to worry about */
  95.     }
  96. }
  97.  
  98. /**************************************************
  99. *
  100. * SaveTheMovie(theMovie) Saves the movie
  101. *
  102. ***************************************************/
  103. void SaveTheMovie(MovieInstance *theMovie)
  104.  
  105. {
  106.         theErr = UpdateMovieResource(theMovie->movie,theMovie->resRefNum,theMovie->resID,nil);
  107.  
  108.         if (theErr)                                /* Put up a message if it didn't work */
  109.             DebugStr((StringPtr)"\pUpdateMovieResource Failed");
  110.  
  111. }
  112.  
  113.  
  114. /**************************************************
  115. *
  116. * (FSSpec* file) 
  117. *    Opens the movie specified by the Standard File Reply 
  118. *
  119. ***************************************************/
  120. void OpenTheMovie(FSSpec* file)
  121. {
  122.     static    short wOffSet = 100;        /* Used to offset the window from the last one opened */
  123.     MovieInstance    *theMovie;
  124.     short     movieResRefNum;
  125.     short     movieResID;
  126.     Str255  movieName;
  127.     Rect    dispBounds;
  128.  
  129.     
  130.     /* First find a space in the movieList Array */ 
  131.     theMovie = GetAvailMovie();
  132.     if (!theMovie)
  133.         goto bail3;
  134.     
  135.     /* Now open the movie file */
  136.     if (theErr = OpenMovieFile(file,  &(theMovie->resRefNum), 0))
  137.         goto bail2;                                /* Bail out if it didn't work */
  138.  
  139.     /* Then the movie in the file */
  140.     theMovie->resID = 0;
  141.     theErr = NewMovieFromFile( &(theMovie->movie),theMovie->resRefNum,&theMovie->resID, nil,0, nil );
  142.  
  143.     if(theErr != 0 || theMovie->movie == 0 ) goto bail;
  144.  
  145.     /* We've got the movie, get the info we need from it */
  146.  
  147.     /* First we need a bounding rectangle for it */
  148.     GetMovieBox(theMovie->movie, &dispBounds);                    /* Get the bounds for the movie */
  149.     OffsetRect(&dispBounds,-dispBounds.left,-dispBounds.top);    /* Make top left 0,0 */
  150.     SetMovieBox(theMovie->movie, &dispBounds);        
  151.  
  152.     theMovie->volume = GetMovieVolume(theMovie->movie);            /* Get the sound volume */
  153.     
  154.     /* Create a window for the movie */
  155.     OffsetRect(&dispBounds,wOffSet,wOffSet);    /* Offset the rect for where we want the window to be */
  156.  
  157.     theMovie->window = (WindowPtr) NewCWindow(0L,&dispBounds,(StringPtr)file->name,0,4,(WindowPtr)-1L,1,0L);
  158.     if(theMovie->window == nil ) goto bail;
  159.  
  160.     SetPort(theMovie->window);                    /* Play the movie in the window */
  161.  
  162.     SetMovieGWorld(theMovie->movie,nil,nil);
  163.     
  164.     SetMovieActive(theMovie->movie, true);                    /* Needs to be active to play */
  165.     GotoBeginningOfMovie(theMovie->movie);
  166.     PrerollMovie(theMovie->movie,0,0);                        /* Get everything ready to play */
  167.  
  168.     MakeMovieControls(theMovie);                            /* Get a controller for the movie */
  169.     ShowWindow(theMovie->window);
  170.  
  171.     MCDoAction(theMovie->movieController, mcActionSetKeysEnabled, (void *)true);
  172.  
  173.     theMovie->idleCount = 8;
  174.     
  175.     numOpenMovies++;                    /* Bump the count of open movies */
  176.  
  177.     wOffSet +=16;                        /* Make next movie window a bit more to the lower right */
  178.     if (wOffSet > 300)
  179.         wOffSet = 100;                    /* But not too far */
  180.  
  181.     return;
  182.     
  183.     /* Cleanup if it didn't work.  A nice guy would put up an alert */
  184.     bail:
  185.     CloseMovieFile(theMovie->resRefNum);                /* We're done with the file */
  186.     
  187.     bail2:
  188.     if(theMovie->movie)
  189.     {
  190.         DisposeMovie(theMovie->movie);
  191.         theMovie->movie = 0;
  192.     }
  193.  
  194.     bail3:
  195.     SysBeep(1);
  196.     return;
  197.  
  198. }
  199.  
  200. /**************************************************
  201. ***************************************************
  202. *
  203. * Movie Control stuff for making and tracking the controls
  204. *
  205. ***************************************************
  206. ***************************************************/
  207.  
  208. /**************************************************
  209. *
  210. * MakeMovieControls(theMovie) Puts the controls at the bottom of the window
  211. *
  212. ***************************************************/
  213. void MakeMovieControls(MovieInstance *theMovie)
  214.  
  215. {
  216.     OSErr    theErr;
  217.     Point    thePoint;
  218.     
  219.     /* Get a controller */
  220.     theMovie->movieController = OpenDefaultComponent('play',0);
  221.  
  222.     if(theMovie->movieController == nil)            /* Did it fail? */
  223.         return;                                
  224.     
  225.     /* Initialize it */
  226.     
  227.     /* Put it in the window */
  228.     thePoint.h = (theMovie->window)->portRect.left;
  229.     thePoint.v = (theMovie->window)->portRect.top;
  230.     theErr = MCNewAttachedController(theMovie->movieController,theMovie->movie,
  231.                     theMovie->window,thePoint);
  232.     
  233.     MCEnableEditing(theMovie->movieController,true);    /* Show the editing controls */
  234.  
  235.     SetMovieWindowSize(theMovie);                        /* Resize the window */
  236.  
  237.     /* Filter for events we want to know about */
  238.     MCSetActionFilter(theMovie->movieController,(MCActionFilter) MyPlayerFilter); 
  239.     
  240. }
  241.  
  242. /**************************************************
  243. *
  244. * SetMovieWindowSize(theMovie)
  245. *    Makes sure the movie box top left corner is 0,0
  246. *    Adjusts the size of the window for the movie box and the controls
  247. *
  248. ***************************************************/
  249. void SetMovieWindowSize(MovieInstance *theMovie)
  250.  
  251. {
  252.     Rect    bounds, controllerBox;
  253.  
  254.     GetMovieBox(theMovie->movie,&bounds);                /* We made the top left 0,0 earlier */
  255.     OffsetRect(&bounds,-bounds.left,-bounds.top);        /* Make sure top left  is 0,0 */
  256.     SetMovieBox(theMovie->movie, &bounds);        
  257.  
  258.     /* Resize the window to be big enough for the movie and the controller */
  259.     MCGetControllerBoundsRect(theMovie->movieController,&controllerBox); /* Get the controller size */
  260.     UnionRect(&bounds,&controllerBox,&bounds);
  261.     SizeWindow(    theMovie->window,bounds.right,bounds.bottom,true);    
  262.     
  263. }        
  264.  
  265. /**************************************************
  266. *
  267. * MyPlayerFilter() Puts the controls at the bottom of the window
  268. *
  269. *    Catches player events I'm interested in
  270. *
  271. *    YOU don't have to do this filter
  272. *   I'm doing it to show how a filter works.
  273. *
  274. *    Activate/  deactivate events are filtered so I can 
  275. *    keep track of the frontmost movie
  276. *
  277. *    The play event is used to loop the movie if the shift key is pressed
  278. *    and palindrome if control is pressed
  279. *
  280. ***************************************************/
  281. pascal Boolean MyPlayerFilter(MovieController pt, short *action, void *params)
  282.  
  283. {
  284.     Boolean    wasHandled = false;
  285.     long    loopFlag;
  286.     short    mCount;
  287.     
  288.     switch( *action)
  289.     {
  290.         case  mcActionActivate:
  291.             DoMovieActivate(&movieList[checkMovieControllerCount]);
  292.             break;
  293.         
  294.         case  mcActionDeactivate:
  295.             DoMovieDeactivate(&movieList[checkMovieControllerCount]);
  296.             break;
  297.  
  298.         case  mcActionPlay:
  299.             loopFlag = (theModifiers & (optionKey | controlKey)) != 0;            /* Option key pressed? */
  300.             theErr = MCDoAction(pt,mcActionSetLooping,(void *) loopFlag);
  301.             loopFlag = (theModifiers & controlKey) != 0;                        /* Control key pressed? */
  302.             theErr = MCDoAction(pt,mcActionSetLoopIsPalindrome,(void *) loopFlag);
  303.             break;    
  304.     }    
  305.     
  306.     return(wasHandled);
  307.  
  308. }
  309.  
  310.  
  311. /**************************************************
  312. ***************************************************
  313. *
  314. * Movie Event stuff
  315. *
  316. ***************************************************
  317. ***************************************************/
  318. /**************************************************
  319. *
  320. * CheckMovieControllers() Checks if the event is handled by any of the 
  321. * controllerss returns true if the event was handled
  322. *
  323. ***************************************************/
  324. Boolean CheckMovieControllers(EventRecord *theEvent)
  325.  
  326. {    
  327.     
  328.     short    mCount;
  329.     Boolean eventHandled = false;    
  330.     
  331.     /* Loop through all of the movies */
  332.     
  333.     for(checkMovieControllerCount = 0;checkMovieControllerCount<maxMovies;checkMovieControllerCount++)
  334.     {    
  335.         if (movieList[checkMovieControllerCount].movie)  {
  336.             if ( movieList[checkMovieControllerCount].idleCount-- == 0 ) 
  337.                 MCDoAction(movieList[checkMovieControllerCount].movieController, mcActionPlay, (void *)kFix1);
  338.             
  339.             if (eventHandled = MCIsPlayerEvent(movieList[checkMovieControllerCount].movieController, theEvent)) /* Was it handled? */
  340.                 break;                                /* If so, return */
  341.         }
  342.     }
  343.  
  344.     return(eventHandled);
  345. }
  346.  
  347. /**************************************************
  348. *
  349. * MyMoviesTask() Calls MoviesTask and stops at the end
  350. *    This is called from the main event loop of the application
  351. *    The active movie is serviced every time to get smooth play
  352. *    The other movies are serviced by the MovieController
  353. *
  354. ***************************************************/
  355. void MyMoviesTask()
  356.  
  357. {    
  358.     
  359.     /* Sevice the activeMovie if there is one */
  360.     if(activeMovie)
  361.         MoviesTask(activeMovie->movie,1);
  362.  
  363. }
  364.  
  365. /**************************************************
  366. *
  367. * MovieMouseDown(theWindow, thePoint, short theModifiers) 
  368. *    Mouse pressed in movie content
  369. *    I don't have anything here since the MovieController does it all for me!
  370. *
  371. ***************************************************/
  372. void MovieMouseDown(WindowPtr theWindow, Point thePoint, short theModifiers)
  373.  
  374. {
  375.  
  376. }
  377.  
  378.     
  379. /**************************************************
  380. *
  381. * DoMovieUpdate()
  382. *
  383. *    Updates the movie screen
  384. *    I don't have anything here since the MovieController does it all for me!
  385. *
  386. ***************************************************/
  387. void DoMovieUpdate(theMovie)
  388. MovieInstance    *theMovie;
  389. {
  390.  
  391. }
  392.  
  393. /**************************************************
  394. *
  395. * DoMovieActivate(theMovie)
  396. *
  397. *    Increases the volume of the front movie
  398. *
  399. ***************************************************/
  400. void DoMovieActivate(MovieInstance *theMovie)
  401.  
  402. {
  403.  
  404.     /* Restore the volume in case it was reduced */
  405.     SetMovieVolume(theMovie->movie,theMovie->volume);
  406.  
  407.     activeMovie = theMovie;
  408.  
  409. }
  410.  
  411. /**************************************************
  412. *
  413. * DoMovieDeactivate(theMovie)
  414. *
  415. *    Reduces the volume of the movie
  416. *
  417. ***************************************************/
  418. void DoMovieDeactivate(MovieInstance *theMovie)
  419.  
  420. {
  421.     /* Save the volume and reduce it */
  422. //    theMovie->volume = GetMovieVolume(theMovie->movie);
  423.     SetMovieVolume(theMovie->movie,theMovie->volume/3);
  424.  
  425.     activeMovie = 0;                    /* no active movie */
  426. }
  427.  
  428. /**************************************************
  429. *
  430. * DoMovieEdit(MovieInstance *theMovie, int menuItem)
  431. *    Handles an edit event for the movie
  432. *
  433. ***************************************************/
  434. void DoMovieEdit(MovieInstance *theMovie, short menuItem)
  435.  
  436. {
  437.     Movie    movieScrap = 0;
  438.     long    offset;
  439.     Rect    bounds,bounds2;
  440.     RgnHandle    tempRgn;
  441.     KeyMap        theKeys;            /* Used to test for shift and control key */
  442.     
  443.     switch (menuItem)
  444.       {
  445.           case    undoItem:
  446.             MCUndo(theMovie->movieController);
  447.             SetMovieWindowSize(theMovie);            /* Resize the window if needed */
  448.             break;
  449.                                 
  450.           case    cutItem:
  451.               movieScrap = MCCut(theMovie->movieController);
  452.             break;
  453.  
  454.         case    copyItem:
  455.              movieScrap = MCCopy(theMovie->movieController);
  456.             break;
  457.  
  458.         case    pasteItem:
  459.             /* First get a movie from the scrap */
  460.             movieScrap = NewMovieFromScrap(0L);
  461.  
  462.             /* Beep and return if there wasn't a movie */
  463.             /* This shouldn't happen since paste is only enabled if there is a movie */
  464.             if(!movieScrap)
  465.             {
  466.                 SysBeep(1);        
  467.                 return;
  468.             }
  469.  
  470.             /* We've got a movie, now do the paste */
  471.             /* Make sure entire movie is used */
  472.             SetMovieSelection(movieScrap,0,GetMovieDuration(movieScrap));
  473.             GetMovieBox(movieScrap,&bounds2);                    /* Adjust the bounds box to be pasted */
  474.             OffsetRect(&bounds2,-bounds2.left,-bounds2.top);    /* Make top left 0,0 */
  475.             SetMovieBox(movieScrap, &bounds2);        
  476.  
  477.             GetMovieBox(theMovie->movie,&bounds);                /* Get the old bounds box */
  478.  
  479.             /* If shift was pressed, set the clip to be the box of the original movie  */
  480.             GetKeys(theKeys);                                    /* Get what keys are presses pressed */
  481.             if ( theKeys[1]&0x01 )
  482.             {
  483.                 tempRgn = NewRgn();
  484.                 RectRgn(tempRgn,&bounds);                        /* Make a region out of the old movie box */
  485.                 SetMovieSrcClipRgn(theMovie->movie,tempRgn);
  486.                 DisposeRgn(tempRgn);
  487.             }
  488.                             
  489.             MCPaste(theMovie->movieController,movieScrap);
  490.  
  491.             SetMovieWindowSize(theMovie);    /* Resize the window if needed */
  492.  
  493.             DisposeMovie(movieScrap);        /* We're all done with the movie */
  494.             movieScrap = 0;
  495.              break;
  496.  
  497.         case    clearItem:
  498.             MCClear(theMovie->movieController);
  499.             break;
  500.         case      selectAllItem:
  501.             {
  502.             TimeRecord tr;
  503.             
  504.             tr.value.hi = tr.value.lo = 0;
  505.             tr.base = 0;
  506.             tr.scale = GetMovieTimeScale(theMovie->movie);
  507.             MCDoAction(theMovie->movieController, mcActionSetSelectionBegin, &tr);
  508.             tr.value.hi = 0;
  509.             tr.value.lo = GetMovieDuration(theMovie->movie);
  510.             tr.base = 0;
  511.             tr.scale = GetMovieTimeScale(theMovie->movie);
  512.             MCDoAction(theMovie->movieController, mcActionSetSelectionDuration, &tr);
  513.             }
  514.             break;
  515.     }
  516.     
  517.     /* If we have a scrap movie, put it in the scap */
  518.     if (movieScrap)
  519.     {
  520.         theErr = PutMovieOnScrap(movieScrap,0L);
  521.         if(theErr)
  522.             DebugStr((StringPtr)"\pPutMovieOnScrap failed");
  523.  
  524.         /* Now throw out the movie */
  525.         DisposeMovie(movieScrap);
  526.     }
  527.         
  528. }
  529.